<?php

class CAppHelper extends CApplicationComponent {

    // 生成图片缩略图
    public function generatePictureThumb($path, $source_file) {
        ini_set("memory_limit", "-1");
        set_time_limit(0);
        $config = Yii::app()->params['upload'];
//        $config = $configs['upload'];
        if (intval($config['generate_thumbnail'])) {
            Yii::import('application.extensions.phpthumb.EasyPhpThumb');
            $thumb = new EasyPhpThumb();
            $thumb->init();
            $thumb->setThumbsDirectory('/upload/media');
            if (isset($config['thumbnail_width'])) {
                $width = (intval($config['thumbnail_width']))
                    ? intval($config['thumbnail_width'])
                    : 200;
            } else {
                $width = 200;
            }
            if (isset($config['thumbnail_height'])) {
                $height = (intval($config['thumbnail_height']))
                    ? intval($config['thumbnail_height'])
                    : 200;
            } else {
                $height = 200;
            }

            try {
//                return $source_file;
                $thumb_file_name = explode('.', $source_file);

                $thumb->load($path . $source_file)
                    ->resize($width, $height)->save("{$thumb_file_name[0]}_thumb" . ".{$thumb_file_name[1]}");
                return "{$thumb_file_name[0]}_thumb" . ".{$thumb_file_name[1]}";
            } catch (Exception $ex) {
                echo $ex;
                return '';
            }
        }
    }

    /**
     * get thumbnail width and height value
     * @param string $object
     * @return array about thumbnail width and height value
     */
    protected static function getThumbnailWidthHeight($object) {
        $width = 200;
        $height = 200;
        $config = Yii::app()->params['upload'];
        $w = $config["thumbnail_{$object}_width"];
        $h = $config["thumbnail_{$object}_height"];
        if (intval($w) >= 30) $width = $w;
        if (intval($h) >= 30) $height = $h;
        return array('width' => $width, 'height' => $height);
    }

    /**
     * 通用图片缩略图生成
     * @param <string> $source_file
     * @param <integer> $width
     * @param <integer> $height
     * @return string Thumbnail name
     */
    public static function generateThumbnail($object, $file_name) {
        ini_set("memory_limit", "-1");
        set_time_limit(0);
        Yii::import('application.extensions.phpthumb.EasyPhpThumb');
        $thumb = new EasyPhpThumb();
        $thumb->init();
        $thumb_directory = "/upload/{$object}";
        $thumb_saved_path = Yii::getPathOfAlias('webroot') . "/upload/{$object}/";
        
        $thumb->setThumbsDirectory($thumb_directory);
        $wh = self::getThumbnailWidthHeight($object);
        $width = $wh['width'];
        $height = $wh['height'];
        try {
            $thumb_file_name = explode('.', $file_name);

            $thumb->load($thumb_saved_path . $file_name)
                ->resize($width, $height)
                ->save("{$thumb_file_name[0]}_thumb" . ".{$thumb_file_name[1]}");
            return "{$thumb_file_name[0]}_thumb" . ".{$thumb_file_name[1]}";
        } catch (Exception $ex) {
            echo $ex;
            exit;
            return '';
        }
    }
}